package com.erpanderp.wam.usm.view.user;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;

import org.primefaces.component.datatable.DataTable;
import org.primefaces.context.RequestContext;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

import com.erpanderp.wam.adm.model.entity.Role;
import com.erpanderp.wam.adm.model.entity.SAPERPModule;
import com.erpanderp.wam.adm.model.service.RoleDAO;
import com.erpanderp.wam.adm.model.service.SAPERPModuleDAO;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.entity.user.EmployeePhoto;
import com.erpanderp.wam.usm.model.entity.user.EmployeeRole;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.usm.model.service.user.EmployeeDAO;
import com.erpanderp.wam.usm.model.service.user.EmployeePhotoDAO;
import com.erpanderp.wam.usm.model.service.user.EmployeeRoleDAO;
import com.erpanderp.wam.util.CommonLookup;

@ManagedBean
@ViewScoped
public class UploadEmployeeImagesBean implements Serializable {

	private static final long serialVersionUID = 1L;
	private DataTable assignedEmployeeImagesTable;

	private String selectedModule;
	private String location;
	private String employeeCode;
	private String employeeName;
	SAPERPModuleDAO saperpModuleDAO;
	private Map<String, String> modulesMap = new HashMap<String, String>();
	
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
			.getExternalContext().getSession(true);
	private WAMUser wamUser = (WAMUser) session
			.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);

	private List<Employee> employees = new ArrayList<Employee>();
	private Employee selectedEmployee;
	private EmployeePhoto employeePhoto = new EmployeePhoto();
	private EmployeePhotoDAO employeePhotoDAO = new EmployeePhotoDAO();
	EmployeeDAO employeeDAO;
	
	private boolean RENDERUPLOADER;

	private UploadedFile file;
	private File realPath;

	public UploadEmployeeImagesBean() {
		if(employeeDAO == null) employeeDAO = new EmployeeDAO();
		if(saperpModuleDAO == null) saperpModuleDAO = new SAPERPModuleDAO();
		
		List<SAPERPModule> saperpModules = saperpModuleDAO.getSAPERPModules();
		
		for(SAPERPModule saperpModule : saperpModules){
			modulesMap.put(saperpModule.getDisplayName(), saperpModule.getDisplayName());
		}
		
		employees = employeeDAO.getAllEmployees();
		System.out.println("============INSIDE CONTR===========");
	}

	public String searchEmployees(){
		System.out.println("Search Action...");
		employees = employeeDAO.getEmployees(selectedModule, location, employeeCode, employeeName);
		if(employees != null && employees.size() > 0){
			System.out.println("EMPLOYEE COUNT: " + employees.size());
		}else{
			
		}	
		return null;
	}

	public void uploadHandler(FileUploadEvent e) {
		ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();
		String fileName = e.getFile().getFileName();
		fileName = selectedEmployee.getEmployeeCode() + fileName.substring(fileName.lastIndexOf("."));
		employeePhoto = new EmployeePhoto();
		employeePhoto.setImagePath(fileName);
		// employeePhoto.setFile(file);
		employeePhoto.setUploadedBy(wamUser.getEmployee().getFirstName());
		employeePhoto.setCreateDate(new Date());
		employeePhoto.setCreatedBy(wamUser.getEmployee().getFirstName());
		employeePhoto.setStatus("ACTIVE");
		selectedEmployee.setPhoto(fileName);
		employeePhoto.setEmployee(selectedEmployee);
		selectedEmployee.getEmployeePhotos().add(employeePhoto);
		
		File rootPath = new File(extContext.getRealPath("//EmployeeImages//"));
		FileOutputStream fileOutputStream;
		try {
			fileOutputStream = new FileOutputStream(rootPath + "/" +  fileName, true);

			byte[] buffer = new byte[1024 * 1024]; // 1MB
			int bulk;
			InputStream inputStream = e.getFile().getInputstream();
			while (true) {
				bulk = inputStream.read(buffer);
				if (bulk < 0) {
					break;
				}
				fileOutputStream.write(buffer, 0, bulk);
				fileOutputStream.flush();
			}
			fileOutputStream.close();
			inputStream.close();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e2) {
			e2.printStackTrace();
		}
		
		employeeDAO.updateEmployee(selectedEmployee);
		employeePhoto = new EmployeePhoto();
	}

	public String showUploaderAction() {
		RENDERUPLOADER = true;
		return null;
	}

	public void closeDialog() {
		System.out.println("Closing...");
		RENDERUPLOADER = false;
		RequestContext.getCurrentInstance().addPartialUpdateTarget(
				"uploaderForm");

	}

	// GENERATE SETTERS AND GETTERS
	public WAMUser getWamUser() {
		return wamUser;
	}

	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}

	public EmployeePhoto getemployeePhoto() {
		return employeePhoto;
	}

	public void setemployeePhoto(EmployeePhoto employeePhoto) {
		this.employeePhoto = employeePhoto;
	}

	public UploadedFile getFile() {
		return file;
	}

	public void setFile(UploadedFile file) {
		this.file = file;
	}

	public DataTable getAssignedEmployeeImagesTable() {
		return assignedEmployeeImagesTable;
	}

	public void setAssignedEmployeeImagesTable(
			DataTable assignedEmployeeImagesTable) {
		this.assignedEmployeeImagesTable = assignedEmployeeImagesTable;
	}

	public File getRealPath() {
		return realPath;
	}

	public void setRealPath(File realPath) {
		this.realPath = realPath;
	}

	public List<Employee> getEmployees() {
		return employees;
	}

	public void setEmployees(List<Employee> employees) {
		this.employees = employees;
	}

	public Employee getSelectedEmployee() {
		return selectedEmployee;
	}

	public void setSelectedEmployee(Employee selectedEmployee) {
		this.selectedEmployee = selectedEmployee;
	}

	public boolean isRENDERUPLOADER() {
		return RENDERUPLOADER;
	}

	public void setRENDERUPLOADER(boolean rENDERUPLOADER) {
		RENDERUPLOADER = rENDERUPLOADER;
	}

	public String getSelectedModule() {
		return selectedModule;
	}


	public void setSelectedModule(String selectedModule) {
		this.selectedModule = selectedModule;
	}


	public String getLocation() {
		return location;
	}


	public void setLocation(String location) {
		this.location = location;
	}


	public String getEmployeeCode() {
		return employeeCode;
	}


	public void setEmployeeCode(String employeeCode) {
		this.employeeCode = employeeCode;
	}


	public String getEmployeeName() {
		return employeeName;
	}


	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}


	public Map<String, String> getModulesMap() {
		return modulesMap;
	}


	public void setModulesMap(Map<String, String> modulesMap) {
		this.modulesMap = modulesMap;
	}
}
